package com.ss.isc.schedule.collect.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.ss.isc.util.DateUtil;
import com.ss.isc.util.SpringUtil;
import com.ss.mapper.LocalCameraMapper;
import com.ss.model.Camera;
import com.ss.thread.CameraThreadPool;
import com.ss.utils.DeviceUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 照片删除定时任务
 * @author 李爽超 chao
 * @create 2019/09/16
 * @email lishuangchao@ss-cas.com
 **/
public class DeletePictruesJob implements SimpleJob {

    public static final Log LOG = LogFactory.getLog(DeletePictruesJob.class);
    private LocalCameraMapper localCameraMapper = SpringUtil.getBean(LocalCameraMapper.class);

    @Override
    public void execute(ShardingContext shardingContext) {
        LOG.info("定时任务DeletePictruesJob已经启动" + DateUtil.getCurrentSqlTimestampString());
        DeletePictrues deletePictrues = new DeletePictrues();
        //放入线程池
        CameraThreadPool.getThread().execute(deletePictrues);
    }
    public class DeletePictrues implements Runnable{
        /**
         * 删除2分钟之前的所有照片
         */
        @Override
        public void run() {
            List<Camera> cutCamera = DeletePictruesJob.this.localCameraMapper.findCutCamera();
            for (Camera camera: cutCamera) {
                String url = DeviceUtil.getCutFlowUrl() + "/" + camera.getCameraId();
                Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 2);
                File folder = new File(url);
                File[] files = folder.listFiles();
                for (int i=0;i<files.length;i++){
                    File file = files[i];
                    if (new Date(file.lastModified()).before(date)){
                        file.delete();
                    }
                }
            }
        }
    }
}


